Interface

  • Note

    What is interface

    1. interface is a contract that defines a set of methods for child classes. Child class must implement / provide their own logic

    2. Interface don't have any properties and function implementations. It contains only function declaration or signature or structure

    When/why should to use interface

    1. to enforce to keep or maintenance the common structure across related classes or unrealted classes

    2. We don't have any shared properties or any shared logics

    How does help the developer

    interfaces helps the developers to design more modular , more flexible, and more maintainable code.

    What are the features

    1. No properties (member variables)

    2. No private or protected functions

    3. no function body / implementation / definition

    4. Completely abstract

    5. no reusable or shared code

    Usage or example

    1. different payment gateway integration in a project

    2. different database services integration in a project

    1. A abstract class has shared properties and shared logics (functions). but in interface, no properties and no shared logics

    2. A abstract class allows single inheritance. But interface allows multiple inheritance

  • Code implemetation

    create an interface using the keyword 'interface'

    
                            interface PaymentInterface{
                                public function processPaymentRequest();
                                public function processPaymentResponse();
                                
                            }
                         

    implement the interface using the keyword 'implement'

    Suppose, we use diferent payment gateways like Strip, Razorpay, Paypal, CCAvenue etc. in our project

    1. Strip payment gateway
    
                         class StripPayment implements PaymentInterface{
    
                            public function processPaymentRequest(){
                                    $stripe = new \Stripe\StripeClient();
                                    $request =  $stripe->checkout->sessions->create();
                            }
    
                             public function processPaymentResponse(){
                                    $stripe = new \Stripe\StripeClient();
                                    $response = $stripe->checkout->sessions->retrieve();
                            }
    
    
                         }   
                  
    2. paypal payment gateway
    
                         class PaypalPayment implements PaymentInterface{
    
                            public function processPaymentRequest(){
                                    $provider = new \PayPal\Services\PayPal;
                                    $request = $provider->createOrder();
                            }
    
                             public function processPaymentResponse(){
                                    $provider = new \PayPal\Services\PayPal;
                                    $response = $provider->capturePaymentOrder();
                            }
    
    
                         }   
                  
    3. razorpay payment gateway
    
                         class RazorpayPayment implements PaymentInterface{
    
                            public function processPaymentRequest(){
                                    $api = new \Razorpay\Api();
                                    $razorpayOrder = $api->order->create();
                            }
    
                             public function processPaymentResponse(){
                                    $api = new \Razorpay\Api();
                                    $response = $api->payment->fetch();
                            }
    
    
                         }   
                  
    Here we focus on code maintainability